package com.sky.controller.admin;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("AdminOrderController")
@RequestMapping("/admin/order")
@Api(tags = "订单相关接口")
public class OrderController {

    private static final Logger log = LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private OrderService orderService;

    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索接口")
    public Result<PageResult> OrderSearch(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("订单查询{}",ordersPageQueryDTO);
        PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> Statistics(){
        log.info("各个状态的订单数量统计");
        OrderStatisticsVO orderStatisticsVO = orderService.Statistics();
        return Result.success(orderStatisticsVO);
    }

    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result<String> OrderConfirm(@RequestBody Orders orders){
        log.info("商家接单，订单id{}",orders);
        orderService.OrderConfirm(orders);
        return Result.success("接单成功");
    }
    @PutMapping("/rejection")
    @ApiOperation("商家拒单")
    public Result<String> OrderRejection(@RequestBody Orders orders){
        log.info("商家拒单{}",orders);
        orderService.OrderRehection(orders);
        return Result.success("拒单成功");
    }

    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result<String> OrderDelivery(@PathVariable String id){
        log.info("派送{}",id);
        orderService.OrderDelivery(Long.valueOf(id));
        return Result.success("派送成功");
    }

    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result<String> OrderComplete(@PathVariable String id){
        log.info("完成订单{}",id);
        orderService.OrderComplete(Long.valueOf(id));
        return Result.success("完成订单成功");
    }

    @GetMapping("/details/{id}")
    @ApiOperation("查询订单详细信息")
    public Result<OrderVO> OrderDetails(@PathVariable String id){
        log.info("查询订单详细信息，id：{}",id);
        OrderVO orderVO = orderService.OrderByid(Long.valueOf(id));
        return Result.success(orderVO);
    }

    @PutMapping("/cancel")
    @ApiOperation("商家取消订单")
    public Result<String> OrderCancel(@RequestBody Orders orders){
        log.info("商家取消订单{}",orders);
        orderService.OrderAdminCancel(orders);
        return Result.success();
    }

}
